﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;

namespace JobCreateInDocs3_TestWinForm
{

    public class SPListPrimaryDocs
    {

        #region Members
        static string _sUrl = "http://localhost/sites/shop/";
        static public string sUrl
        {
            get { return _sUrl; }
        }
        static string _sListName = "Первичные документы";
        static public string sListName
        {
            get { return _sListName; }
        }
        #endregion Members

        public static SPListItem GetItemByTitle(string parStr)
        {
            SPListItem ret = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(sUrl))
                {
                    using (SPWeb mWeb = site.OpenWeb())
                    {
                        try
                        {
                            SPList list = mWeb.Lists[sListName];
                            SPQuery query = new SPQuery();
                            query.Query =
@"<Where><Eq><FieldRef Name='Title'/><Value Type='Text'>" + parStr + "</Value></Eq></Where>";
                            SPListItemCollection myItems = list.GetItems(query);
                            foreach (SPListItem currentItem in myItems)
                            {
                                // CreateLineLog("Doc: " + currentItem["Title"].ToString() + " Shop: " + currentItem["Магазин"].ToString());
                                if (currentItem["Title"].ToString() == parStr)
                                {
                                    ret = currentItem;
                                    break;
                                }
                            }
                        }
                        catch
                        {
                        }
                    }
                }
            });
            return ret;
        }

        public static Exception GetItemById(ref SPListItem parItem, int parID)
        {
            Exception ret = null;
            SPListItem item = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(sUrl))
                {
                    using (SPWeb mWeb = site.OpenWeb())
                    {
                        try
                        {
                            SPList list = mWeb.Lists[sListName];
                            item = list.GetItemById(parID);
                        }
                        catch(Exception err)
                        {
                            ret = new Exception(err.Message, err);
                        }
                    }
                }
            });
            parItem = item;
            return ret;
        }

        public static Exception GetItemByAxId(ref SPListItem parItem, Int64 parAxID)
        {
            Exception ret = null;
            SPListItem item = null;
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite site = new SPSite(sUrl))
                {
                    using (SPWeb mWeb = site.OpenWeb())
                    {
                        try
                        {
                            SPList list = mWeb.Lists[sListName];
                            SPQuery query = new SPQuery();
                            query.Query =
@"<Where><Eq><FieldRef Name='AxID'/><Value Type='Number'>" + parAxID + "</Value></Eq></Where>";
                            SPListItemCollection myItems = list.GetItems(query);
                            foreach (SPListItem currentItem in myItems)
                            {
                                // CreateLineLog("Doc: " + currentItem["Title"].ToString() + " Shop: " + currentItem["Магазин"].ToString());
                                if (Convert.ToInt64(currentItem["AxID"]) == parAxID)
                                {
                                    item = currentItem;
                                    break;
                                }
                            }
                        }
                        catch(Exception err)
                        {
                            ret = new Exception(err.Message, err);
                        }
                    }
                }
            });
            parItem = item;
            return ret;
        }

    }

}
